package edu.purdue.cs505;

/*
 * Internal class used to create and serialize an ACK message for a packet with the given id.
 */
public class AckMessage
{
   private long idToAck;

   public AckMessage(long idToAck)
   {
      this.idToAck = idToAck;
   }

   public String serialize()
   {
      return Constants.ACK_PREFIX + Constants.SEPARATOR + idToAck;
   }

   public long getId()
   {
      return idToAck;
   }

   @Override
   public String toString()
   {
      return serialize();
   }

   public static boolean isAck(String message)
   {
      return message.startsWith(Constants.ACK_PREFIX);
   }

   public static AckMessage deserialize(String message)
   {
      Long id =
               Long.parseLong(message.substring(message.indexOf(Constants.SEPARATOR) + 1,
                        message.indexOf("\0")));
      return new AckMessage(id.longValue());
   }
}
